package com.example.pingze.controller;

import com.example.pingze.entity.RecommendPicture;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.RecommendPictureService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/recommendPicture")
@CrossOrigin(origins = "*")
public class RecommendPictureController {
    @Autowired
    private RecommendPictureService service;
    @PostMapping("/getPicture")
    public Result<String> getPicture(@RequestParam String pictureTime){
        if(pictureTime == null){
            throw new PingZeException("未获取日期");
        }
        RecommendPicture picture = service.findRecommendPictureByPictureTime(pictureTime);
        String pictureUrl = picture.getPictureUrl();
        System.out.println(pictureTime);
        return ResultVOUtils.result(pictureUrl);
    }
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<RecommendPicture>> getList(
            RecommendPicture recommendPicture,
            @RequestParam(name = "pageCode", required = false, defaultValue = "1") String pageCode,
            @RequestParam(name = "pageSize", required = false, defaultValue = "2") String pageSize
    ){
        int i = Integer.parseInt(pageCode);
        int j = Integer.parseInt(pageSize);
        if(i < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order pictureTimeOrder = Sort.Order.asc("pictureTime");
        Sort sort = Sort.by(pictureTimeOrder);
        Pageable pageable = PageRequest.of(i - 1, j, sort);
        Page page = service.getRecommendPictureList(recommendPicture, pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<RecommendPicture> add(String pictureUrl, String pictureTime){
        RecommendPicture add = service.add(pictureUrl,pictureTime);
        return ResultVOUtils.result(add);
    }
    @CrossOrigin
    @PostMapping("/edit")
    public Result<RecommendPicture> edit(String id, String pictureUrl, String pictureTime){
        RecommendPicture edit = service.edit(id,pictureUrl,pictureTime);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        RecommendPicture find = service.findRecommendPictureById(id);
        if(find != null){
            service.delete(find);
        }
        return ResultVOUtils.success("delete successful");
    }
}
